package com.atguigu.yygh.apartment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.apartment.repository.ApartmentRepository;
import com.atguigu.yygh.apartment.service.ApartmentInfoService;
import com.atguigu.yygh.apartment.service.ApartmentService;
import com.atguigu.yygh.model.hosp.*;
import com.atguigu.yygh.vo.hosp.ApartmentInfoQueryVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jscq.cmn.client.DictFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ApartmentInfoServiceImpl implements ApartmentInfoService {

    @Autowired
    private ApartmentRepository apartmentRepository;

    @Autowired
    private DictFeignClient dictFeignClient;


    @Override
    public void save(Map<String, Object> paramMap) {
        System.out.println("paramMap:"+paramMap.toString());
        //把map集合转换成对象 ApartmentInfo
        String mapString = JSONObject.toJSONString(paramMap);
        ApartmentInfo apartmentInfo = JSONObject.parseObject(mapString, ApartmentInfo.class);
        //判断是否存在数据
        String apartmentCode = apartmentInfo.getApartmentCode();
        ApartmentInfo apartmentInfoExist = apartmentRepository.getApartmentInfoByApartmentCode(apartmentCode);
        //如果不存在，进行添加
        if (apartmentInfoExist!=null) {
            apartmentInfo.setStatus(apartmentInfoExist.getStatus());
            apartmentInfo.setCreateTime(apartmentInfoExist.getCreateTime());
            apartmentInfo.setUpdateTime(new Date());
            apartmentInfo.setIsDeleted(0);
            apartmentRepository.save(apartmentInfo);
        }else {
            apartmentInfo.setStatus(0);
            apartmentInfo.setCreateTime(new Date());
            apartmentInfo.setUpdateTime(new Date());
            apartmentInfo.setIsDeleted(0);
            apartmentRepository.save(apartmentInfo);
        }
        //存在，修改
    }

    @Override
    public void save(ApartmentInfoEvVo apartmentInfoEvVo) throws JsonProcessingException {
        ApartmentInfo apartmentInfo = new ApartmentInfo();
        if (apartmentInfo.getApartmentType()=="男"){
            apartmentInfo.setApartmentType("1");
        }else {
            apartmentInfo.setApartmentType("2");
        }
        BeanUtils.copyProperties(apartmentInfoEvVo,apartmentInfo);
        //判断是否存在数据
        String apartmentCode = apartmentInfo.getApartmentCode();
        ApartmentInfo apartmentInfoExist = apartmentRepository.getApartmentInfoByApartmentCode(apartmentCode);
        apartmentInfo.setProvinceCode(apartmentInfoEvVo.getAddressSelected().get(0));
        apartmentInfo.setCityCode(apartmentInfoEvVo.getAddressSelected().get(1));
        apartmentInfo.setDistrictCode(apartmentInfoEvVo.getAddressSelected().get(2));
        BookingRule bookingRule = new BookingRule();
        bookingRule.setCycle(apartmentInfoEvVo.getBookingRule().getCycle());
        bookingRule.setQuitDay(apartmentInfoEvVo.getBookingRule().getQuitDay());
        bookingRule.setQuitTime(apartmentInfoEvVo.getBookingRule().getQuitTime());
        bookingRule.setReleaseTime(apartmentInfoEvVo.getBookingRule().getReleaseTime());
        bookingRule.setStopTime(apartmentInfoEvVo.getBookingRule().getStopTime());
        List<String> rule = new ArrayList<>();
        rule.add(apartmentInfoEvVo.getBookingRule().getRule());

        ObjectMapper objectMapper = new ObjectMapper();
        String ruleJson = objectMapper.writeValueAsString(rule);
        bookingRule.setRule(ruleJson);
        String json = objectMapper.writeValueAsString(bookingRule);

        System.out.println("json:"+json);
        //BeanUtils.copyProperties(apartmentInfoEvVo.getBookingRule(),bookingRule);
        //System.out.println("bookingRule:"+bookingRule);

        if (apartmentInfoExist!=null) {
            apartmentInfoExist.setStatus(apartmentInfoExist.getStatus());
            apartmentInfoExist.setCreateTime(apartmentInfoExist.getCreateTime());
            apartmentInfoExist.setUpdateTime(new Date());
            apartmentInfoExist.setIsDeleted(0);
            apartmentInfoExist.setBookingRule(json);
            apartmentInfoExist.setApartmentType(apartmentInfo.getApartmentType());
            apartmentInfoExist.setCityCode(apartmentInfo.getCityCode());
            apartmentInfoExist.setDistrictCode(apartmentInfo.getDistrictCode());
            apartmentInfoExist.setProvinceCode(apartmentInfo.getProvinceCode());
            apartmentInfoExist.setAddress(apartmentInfo.getAddress());
            apartmentInfoExist.setIntro(apartmentInfo.getIntro());
            apartmentInfoExist.setLogoData(apartmentInfo.getLogoData());
            apartmentInfoExist.setApartmentName(apartmentInfo.getApartmentName());
            apartmentInfoExist.setRoute(apartmentInfo.getRoute());

            apartmentRepository.save(apartmentInfoExist);
        }else {//如果不存在，进行添加
            apartmentInfo.setStatus(0);
            apartmentInfo.setCreateTime(new Date());
            apartmentInfo.setUpdateTime(new Date());
            apartmentInfo.setIsDeleted(0);
            apartmentInfo.setBookingRule(json);
            apartmentRepository.save(apartmentInfo);
        }
        System.out.println("apartmentInfo:"+apartmentInfo);
    }

    @Override
    public ApartmentInfo getByApartmentCode(String apartmentCode) {
        ApartmentInfo apartmentInfo = apartmentRepository.getApartmentInfoByApartmentCode(apartmentCode);
        return apartmentInfo;
    }

    //宿舍列表 分页
    @Override
    public Page<ApartmentInfo> selectApartmnetInfoPage(Integer page, Integer limit, ApartmentInfoQueryVo apartmentInfoQueryVo) {

        //pageable对象
        Pageable pageable= PageRequest.of(page-1,limit);
        //条件
        ExampleMatcher exampleMatcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //转换对象
        ApartmentInfo apartmentInfo=new ApartmentInfo();
        BeanUtils.copyProperties(apartmentInfoQueryVo,apartmentInfo);

        //创建对象
        Example<ApartmentInfo> example=Example.of(apartmentInfo,exampleMatcher);
        //调用方法查询实现
        Page<ApartmentInfo> pages = apartmentRepository.findAll(example, pageable);

        pages.getContent().stream().forEach(item ->{
            this.setApartmentInfoApartmentType(item);
        });
        return pages;
    }

    //更新状态
    @Override
    public void updateStatus(String id, Integer status) {
        //获得宿舍
        ApartmentInfo apartmentInfo = apartmentRepository.findById(id).get();
        //设置修改的值
        apartmentInfo.setStatus(status);
        apartmentInfo.setUpdateTime(new Date());
        apartmentRepository.save(apartmentInfo);
    }
    //获得宿舍详情
    @Override
    public Map<String,Object> getApartmentInfoById(String id) {
        ApartmentInfo apartmentInfo = this.setApartmentInfoApartmentType(apartmentRepository.findById(id).get());
        Map<String,Object> result = new HashMap<>();
        result.put("apartmentInfo",apartmentInfo);

        result.put("bookingRule",apartmentInfo.getBookingRule());
        apartmentInfo.setBookingRule(null);
        return result;
    }

    @Override
    public List<ApartmentInfo> findByApartmentName(String apartmentName) {
        return apartmentRepository.findApartmentInfoByApartmentNameLike(apartmentName);
    }
    //根据编号得到详情
    @Override
    public Map<String, Object> findApartmentDeatil(String apartmentCode) {
        Map<String,Object> result = new HashMap<>();
        //详情
        ApartmentInfo apartmentInfo = this.setApartmentInfoApartmentType(this.getByApartmentCode(apartmentCode));
        //查询省 市 地区
        String provinceString = dictFeignClient.getName(apartmentInfo.getProvinceCode());
        String cityString = dictFeignClient.getName(apartmentInfo.getCityCode());
        String districtString = dictFeignClient.getName(apartmentInfo.getDistrictCode());
        result.put("apartmentInfoAddress",provinceString+"/"+cityString+"/"+districtString);
        result.put("apartmentInfo", apartmentInfo);
        result.put("bookingRule",apartmentInfo.getBookingRule());
        apartmentInfo.setBookingRule(null);
        return result;
    }

    //获取查询list集合，遍历封装宿舍类型
    private ApartmentInfo setApartmentInfoApartmentType(ApartmentInfo item) {
        //根据dictCode and value 获得 宿舍类型名称
        String apartmentTypeString = dictFeignClient.getName("apartmentType", item.getApartmentType());
        //查询省 市 地区
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String districtString = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("apartmentType",apartmentTypeString);
        item.getParam().put("fullAddress",provinceString+cityString+districtString);
        return item;
    }
}
